/**
 * Project: topdt-jbpm
 * Copyright 2013  Top Digital Technology  Co. Ltd.
 * All right reserved.
 */
package com.topdt.jbpm.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.thx.common.orm.HibernateBaseDao;
import com.thx.common.util.AppContext;
import com.topdt.jbpm.model.WorkAttach;

/**
 * 建立日期 : 2013-4-25 下午12:03:16<br>
 * 作者 : balance<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
@Repository
public class AttachmentDao extends HibernateBaseDao<WorkAttach> {
	/**
	 * 根据工作id取得附件列表 建立日期 : June 25, 2013 5:41:50 PM<br>
	 * 作者 : wangamin<br>
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<WorkAttach> getWorkAttachByWorkId(String workId) {
		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = "companyId='" + comId + "' and ";
		String hql = "from WorkAttach where " + condition + "workId = ? order by uploadTime desc";
		List<WorkAttach> list = createQuery(hql, workId).list();
		if (list.size() > 0) {
			return list;
		}
		return null;
	}

	public void deleteAttach(String attachId) {
		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = "companyId='" + comId + "' and ";
		String hql = "delete from WorkAttach where " + condition + "attachId = ?";
		createQuery(hql, attachId).executeUpdate();

	}

	/**
	 * 根据附件id查询附件 建立日期 : July 16, 2013 5:41:50 PM<br>
	 * 作者 : wangamin<br>
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public WorkAttach getWorkAttachByAttachId(String attachId) {
		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = "companyId='" + comId + "' and ";
		String hql = "from WorkAttach where " + condition + " attachId = ?";
		List<WorkAttach> list = createQuery(hql, attachId).list();
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 根据附件路径查询附件 建立日期 : July 16, 2013 5:41:50 PM<br>
	 * 作者 : wangamin<br>
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public WorkAttach getWorkAttachByFullName(String dstPath) {
		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = "companyId='" + comId + "' and ";
		String hql = "from WorkAttach where " + condition + "nowFullName = ?";
		List<WorkAttach> list = createQuery(hql, dstPath).list();
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 根据附件FileId查询附件 建立日期 : July 16, 2013 5:41:50 PM<br>
	 * 作者 : wangamin<br>
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public WorkAttach getWorkAttachByFileId(String fileId) {
		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = "companyId='" + comId + "' and ";
		String hql = "from WorkAttach where " + condition + "fileId = ?";
		List<WorkAttach> list = createQuery(hql, fileId).list();
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	/**
	 * 
	* @author _zili
	* @Description:如果多个附件一起上传，则会出现保存fileId一样的情况，所以再多传个name进行查询。
	* @date 2015年5月6日 上午10:38:55
	 */
	@SuppressWarnings("unchecked")
	public WorkAttach getWorkAttachByFileIdAndName(String fileId,String realName) {
		List<Object> params = new ArrayList<Object>();
		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (comId != null && !"".equals(comId)){
			condition = "companyId = ? and ";
			params.add(comId);
		}
		String hql = "from WorkAttach where " + condition + "fileId = ? and realName = ?";
		params.add(fileId);
		params.add(realName);
		List<WorkAttach> list = createQuery(hql, params.toArray()).list();
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	public void delByWorkId(String workId) {

		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = "companyId='" + comId + "' and ";

		String hql = "delete from WorkAttach where " + condition + " workId=?";
		this.createQuery(hql, workId).executeUpdate();
	}
	
	/**
	 * 建立日期 : Aug 10, 2013 1:54:23 PM<br>
	 * 作者: 孙耀飞<br>
	 * 模块: 根据条件查询附件 <br>
	 * 描述: realName:实际名字；handlerName:处理者;todoId:待办ID<br>
	 * 1 <br>
	 * 2 <br>
	 */
	@SuppressWarnings("unchecked")
	public List<WorkAttach> getAttachByCondition(String realName,String handlerName,String todoId) {
		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = "companyId='" + comId + "' and ";
		String hql = "from WorkAttach where " + condition + "realName = ? and handlerName =? and todoId=? order by uploadTime desc";
		List<WorkAttach> list = createQuery(hql,new Object[]{realName,handlerName,todoId}).list();
		
		return list;
	}
	
	/**
	 * 建立日期 : Aug 10, 2013 1:54:23 PM<br>
	 * 作者: 孙耀飞<br>
	 * 模块: 根据条件查询附件 <br>
	 * 描述: tempName:临时名字；handlerName:处理者;todoId:待办ID<br>
	 * 1 <br>
	 * 2 <br>
	 */
	@SuppressWarnings("unchecked")
	public List<WorkAttach> getAttachList(String tempName,String handlerName,String todoId) {
		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (comId != null && !"".equals(comId))
			condition = "companyId='" + comId + "' and ";
		String hql = "from WorkAttach where " + condition + "tempName = ? and handlerName =? and todoId=? order by uploadTime desc";
		List<WorkAttach> list = createQuery(hql,new Object[]{tempName,handlerName,todoId}).list();
		
		return list;
	}

	/**
	 * 根据work_id删除wf_work_attach表中记录
	 * @author 刘杰
	 * @date   May 16, 2014 3:51:18 PM 
	 * @param delId
	 */
	public void deleteWorks(String delIdsStr) {
		String sql = "delete from wf_work_attach where work_id   in   ("+delIdsStr+") ";
		createSQLQuery(sql).executeUpdate();
		
	}
	

	/**
	 * 根据文件id查询数据<br>
	 * 建立日期 : 20141205<br>
	 * 作者 : BY LCX
	 * @param folderId
	 * @return
	 */
	
	public WorkAttach findById(String fileId){
		String hql = "from WorkAttach where fileId = '"+fileId+"'";
		WorkAttach fileData = new WorkAttach();
		List<WorkAttach> list = (List<WorkAttach>) createQuery(hql).list();
		if(list.size()>0){
			fileData = list.get(0);
		}
		return fileData;
	}
	
	/**
	 * 
	 * 建立日期 : 2014-12-12 下午4:50:53<br>
	 * 作者 : LCX<br>
	 * 模块 : <br>
	 * 描述 : 查询所有上传过的文件<br>
	 * 修改历史: 序号 日期 修改人 修改原因 <br>
	 * @return
	 * 1 <br>
	 * 2 <br>
	 */
	public List<WorkAttach> getAllWorkAttach() {
		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (comId != null && !"".equals(comId))
		{
			condition = "w.company_id='" + comId + "'  and ";
		}
		String sql = "select W.ATTACH_ID, W.WORK_ID, W.TODO_ID,W.ACTIVITY_NAME, W.HANDLER_NAME, W.REAL_NAME, W.NOW_FULL_NAME, "
				+ "W.UPLOAD_TIME, W.COMPANY_ID, W.FILE_ID, W.TEMP_NAME, W.SWF_FULL_NAME from wf_work_attach w where "
				+ condition + " w.now_full_name is not null and w.swf_full_name is null";
		List<WorkAttach> list = createSQLQuery(sql).addEntity(WorkAttach.class).list();
		return list;
	}
	
	/**
	 * 更新数据库
	 * @author LCX
	 * @date 20141209
	 * @param personnelId
	 * @param nowTime
	 * @return
	 */
	public void updateWorkAttachment(String swfFullName,String attachId) {
		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (comId != null && !"".equals(comId))
		{
			condition = " w.company_id='" + comId + "'  and ";
		}
		String sql = "update wf_work_attach w set w.swf_full_name = '"+swfFullName+"' where "+condition+" w.attach_id = '"+attachId+"'";
		createSQLQuery(sql).executeUpdate();
	}
	

}